#
#    Copyright 2006 Andrew Wilkinson <aw@cs.york.ac.uk>.
#
#    This file is part of PyScobble (http://lastfm.indiegigs.co.uk/pyscrobble)
#
#    PyScobble is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 2.1 of the License, or
#    (at your option) any later version.
#
#    PyScobble is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with PyScobble; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

from scrobble.utils import *
from scrobble.database import getSimilarArtists

## A Last.fm Artist
#
# @param name The name of the artist.
# @param mbid A string containing the MusicBrainz ID of this artist.
class Artist(object):
    def __init__(self, name, mbid=None):
        self.__name = name
        self.__mbid = mbid

        self.__profile = {"name": name}

    def getLink(self):
        return """<a href="http://www.last.fm/music/%s">%s</a>""" % (self.name, self.name)

    def __getName(self):
        return self.__name
    name = property(__getName)
    def __getMbId(self):
        return self.__mbid
    mbid = property(__getMbId)

    def __getSimilar(self):
        if self.__profile.has_key("similar"):
            return self.__profile["similar"]

        similar = getSimilarArtists(self.__name)
        self.__profile["similar"] = [Artist(f) for f in similar]
        return self.__profile["similar"]
    similar = property(__getSimilar)

    def __getGlobalTags(self):
        if self.__profile.has_key("global_tags"):
            return self.__profile["global_tags"]
        dom = getDom(audioscrobbler_webservices, "/1.0/artist/%s/toptags.xml" % (encode(self.name.replace(" ", "+")), ))

        r = []
        tags = dom.getElementsByTagName("toptags")[0]
        for t in tags.getElementsByTagName("tag"):
            r.append(getDomText(t.getElementsByTagName("name")[0].childNodes))
        return r
    global_tags = property(__getGlobalTags)

    def tags(self, user=None):
        if user is None:
            return self.global_tags
        if not isinstance(user, User):
            raise TypeError, "The given user must be either None or a User, not %s." % (type(user), )
        dom = getDom(audioscrobbler_webservices, "/1.0/user/%s/artisttags.xml?artist=%s" % (encode(user.username.replace(" ", "+")), encode(self.name.replace(" ", "+"))))

        r = []
        tags = dom.getElementsByTagName("artisttags")[0]
        for t in tags.getElementsByTagName("tag"):
            r.append(getDomText(t.getElementsByTagName("name")[0].childNodes))
        return r

    def __hash__(self):
        return hash(self.name)
    def __cmp__(self, other):
        if self.mbid is not None and other.mbid is not None:
            return cmp(self.mbid, other.mbid)
        else:
            return cmp(self.name, other.name)

    def __str__(self):
        return "%s" % (self.name, )
    def __repr__(self):
        return "<scrobble.Artist(%s)" % (repr(self.name), )

from scrobble.user import User
